package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.BaseRLSJBean;
import com.koron.css2.baseConfig.mapper.BaseRLSJMapper;
import com.koron.css2.chargeCenter.bean.BillQuotaBean;
import com.koron.css2.chargeCenter.mapper.BillQuotaMapper;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.meterService.bean.MsInfoBean;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Objects;

public class BaseRLSJUpdate implements ServerInterface {


    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            BaseRLSJBean bean = JsonUtils.objectToPojo(req.getData(), BaseRLSJBean.class);
            BaseRLSJMapper mapper = factory.getMapper(BaseRLSJMapper.class);
            //先删除口径、用水类型对应的数据，然后重新添加
            List<String> useWaterTypes = bean.getUseWaterTypes();
            for (String useWaterType:useWaterTypes) {
                BaseRLSJBean insertBean = new BaseRLSJBean();
                CommonUtils.copyPropertiesIgnoreNull(bean,insertBean);
                insertBean.setCreateInfo(userInfo);
                insertBean.setUpdateInfo(userInfo);
                insertBean.setUseWaterType(useWaterType);
                //先删再加
                mapper.del(insertBean);
                mapper.add(insertBean);
            }

        } catch (Exception e) {
            logger.error("操作失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "操作失败", void.class);
        }
        return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
    }

    /**
     * 按口径、用水类型计算用户的容量水费
     * @param beans
     * @return
     */
    public static void calculateRLSJ(SessionFactory factory, UserInfoBean userInfo, UserBean user, List<BaseRLSJBean> beans){
        //获取水表口径
        MsInfoMapper meterMapper = factory.getMapper(MsInfoMapper.class);
        MsInfoDto msInfo = meterMapper.getMsInfoByNo(user.getMeterNo());
        Objects.requireNonNull(msInfo,"水表信息不存在");
        //获取子费用类型
        BillQuotaMapper mapper = factory.getMapper(BillQuotaMapper.class);

        //口径及用水类型能对上且金额>0才生成
        for (BaseRLSJBean bean:beans) {
            if(StringUtils.isNotBlank(bean.getBoreId())
                    && bean.getBoreId().equals(msInfo.getMeterBore())
                    && bean.getUseWaterType().equals(user.getUseWaterType())
                    && bean.getRlsj()>0){
                BillQuotaBean result = new BillQuotaBean();
                result.setUserNo(user.getUserNo());
                result.setCostNo("20");
                result.setSubCostNo("2001");
                result.setQuotaMoney(bean.getRlsj());

                result.setZeroWaterCalc(1);//0水量计费
                result.setCalcWay(1);//按定额计费

                result.setCreateInfo(userInfo);
                //删除原记录后重新生成
                mapper.deleteBillQuota(result.getUserNo(), result.getSubCostNo());
                mapper.insertBillQuota(result);
                break;
            }
        }
    }
    /**
     * 按口径、用水类型计算用户的容量水费
     * @param beans
     * @return
     */
    public static void calculateRLSJ(SessionFactory factory, UserInfoBean userInfo, UserBean user, MsInfoBean msInfo, List<BaseRLSJBean> beans){
        //获取水表口径
        MsInfoMapper meterMapper = factory.getMapper(MsInfoMapper.class);
        //获取子费用类型
        BillQuotaMapper mapper = factory.getMapper(BillQuotaMapper.class);

        //口径及用水类型能对上且金额>0才生成
        for (BaseRLSJBean bean:beans) {
            if(StringUtils.isNotBlank(bean.getBoreId())&& bean.getBoreId().equals(msInfo.getMeterBore())
                    && bean.getUseWaterType().equals(user.getUseWaterType()) && bean.getRlsj()>0){
                BillQuotaBean result = new BillQuotaBean();
                result.setUserNo(user.getUserNo());
                result.setCostNo("20");
                result.setSubCostNo("2001");
                result.setQuotaMoney(bean.getRlsj());

                result.setZeroWaterCalc(1);//0水量计费
                result.setCalcWay(1);//按定额计费

                result.setCreateInfo(userInfo);
                //删除原记录后重新生成
                mapper.deleteBillQuota(result.getUserNo(), result.getSubCostNo());
                mapper.insertBillQuota(result);
                break;
            }
        }
    }
}
